package com.gitee.ixtf.vertx.internal.converter

import com.gitee.ixtf.vertx.ParameterConverter
import graphql.schema.DataFetchingEnvironment
import java.lang.reflect.Parameter
import org.eclipse.microprofile.graphql.DefaultValue
import org.eclipse.microprofile.graphql.Name

class NameParameterConverter(
    private val parameter: Parameter,
) : ParameterConverter<Any?> {
  private val key by lazy { parameter.getAnnotation(Name::class.java).value }
  private val defaultValue by lazy { parameter.getAnnotation(DefaultValue::class.java)?.value }
  private val converter by lazy { implicitConverter(parameter) }
  private val convert by lazy { defaultValue?.let { converter.convert(it) } }

  override suspend fun convert(env: DataFetchingEnvironment) = env.arguments[key] ?: convert
}
